package com.pro_one.controller;

import com.pro_one.constant.MessageConstant;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.entity.Result;
import com.pro_one.pojo.Meals;
import com.pro_one.service.MealsService;
import com.pro_one.util.POIUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
* 膳食库*/
@RestController
@RequestMapping("meals")
public class MealsController {
@Reference
    private MealsService mealsService;
    /*上传文件 */
    @RequestMapping("upload")
    public void upload(@RequestParam("excelFile") MultipartFile excelFile){

        try {
            List<String[]> strings = POIUtils.readExcel(excelFile);
            if (strings!=null&&strings.size()>0) {
                List<Meals> list = new ArrayList<>();
                for (String[] string : strings) {
                    String number = string[0];
                    String name = string[1];
                    String plain = string[2];
                    String foodkcal = string[3];
                    String foodkj = string[4];
                    String foodprotein = string[5];
                    String foodfat = string[6];
                    String foodunit = string[7];
                    String age = string[8];
                    list.add(new Meals(number, name, plain, foodkcal, foodkj, foodprotein, foodfat, foodunit, age));
                }
                mealsService.upload(list);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("findByCondition")
    public PageResult findByCondition(@RequestBody QueryPageBean pageBean){

        return mealsService.findByCondition(pageBean);

    }
    /*增加*/
    @RequestMapping("add")
    public Result add(@RequestBody Meals meals){
        try {
            mealsService.add(meals);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MEALS_FAIL);
        }
        return new Result(true, MessageConstant.ADD_MEALS_SUCCESS);
    }
}
